<?php require_once('common.php');
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Cs extends Common {
	function __construct() {
		parent::__construct("cs");
		$this->load->model(array('kuisioner'));
	}

	public function index(){
		$kuisioner = $this->kuisioner->get()->result_array();
		for($i = 0; $i < count($kuisioner); $i++){
			$kuisioner[$i]['pilihan'] = $this->kuisioner->get_pilihan_jawaban(array('id_kuisioner' => $kuisioner[$i]['id_kuisioner']))->result_array();
		}
		$data['kuisioner'] = $kuisioner;
		$data['pesan'] = $this->session->flashdata('pesan');
		$this->load->view('cs/list_kuisioner', $data);
	}
	
	function add_kuisioner(){
		$data['mode'] = 'ADD';
		$this->load->view('cs/form_kuisioner',$data);
	}

	function edit_kuisioner($id){
		$data['mode'] = 'EDIT';
		$data['detail'] = $this->kuisioner->get(array('id_kuisioner' => $id))->row_array();
		$data['pilihan'] = $this->kuisioner->get_pilihan_jawaban(array('id_kuisioner' => $id))->result_array();
		$this->load->view('cs/form_kuisioner',$data);	
	}
	
	function save_kuisioner(){
		$this->layout = FALSE;
		$mode = $this->input->post('mode');
		$data = array('pertanyaan'	=> $this->input->post('pertanyaan'),
					  'status' 		=> $this->input->post('status'),
					  'id_petugas' 	=> $this->user_data['id_petugas']);
		
		if($mode == 'ADD'){
			$id = $this->kuisioner->add($data);
			$success = $id;
		}else{
			$id = $this->input->post('id_kuisioner');
			$success = $this->kuisioner->edit($id, $data);
		}
		
		//pilihan jawaban kuisioner
		$pilihan = $this->input->post('pilihan');
		$id_pilihan = $this->input->post('id_pilihan');
		if($pilihan != NULL){			
			for($j = 0; $j < count($pilihan); $j++){
				if($pilihan[$j] != ""){
					if($id_pilihan[$j] != ""){
						$inp = array('isi' => $pilihan[$j]);
						$this->kuisioner->edit_pilihan_jawaban($id_pilihan[$j], $inp);
					}else{
						$inp = array('id_kuisioner' => $id,
									 'isi' => $pilihan[$j]);
						$this->kuisioner->add_pilihan_jawaban($inp);
					}
				}
			}
		}
		
		if($success){
			$this->session->set_flashdata('pesan', array('success' =>true, 'pesan' => "Data Kuisioner berhasil disimpan."));
		}else{
			$this->session->set_flashdata('pesan', array('success' =>false, 'pesan' => "Data Kuisioner gagal disimpan."));
		}
		redirect(base_url()."index.php/cs");
	}
	
	function delete_kuisioner($id){
		if($this->kuisioner->delete($id)){
			$this->session->set_flashdata('pesan', array('success' =>true, 'pesan' => "Kuisioner berhasil dihapus."));
		}else{
			$this->session->set_flashdata('pesan', array('success' =>false, 'pesan' => "Kuisioner gagal dihapus."));
		}
		redirect($_SERVER['HTTP_REFERER']);
	}
	
	function edit_status($id_kuisioner, $status){
		if($this->kuisioner->edit($id_kuisioner, array('status' => $status))){
			$this->session->set_flashdata('pesan', array('success' =>true, 'pesan' => "Status Kuisioner berhasil diubah."));
		}else{
			$this->session->set_flashdata('pesan', array('success' =>false, 'pesan' => "Status Kuisioner gagal diubah."));
		}
		redirect($_SERVER['HTTP_REFERER']);
	}
	
	function isi_kuisioner(){
		$kuisioner = $this->kuisioner->get(array('status' => STATUS_KUISIONER_PUBLISH))->result_array();
		for($i = 0; $i < count($kuisioner); $i++){
			$kuisioner[$i]['pilihan'] = $this->kuisioner->get_pilihan_jawaban(array('id_kuisioner' => $kuisioner[$i]['id_kuisioner']))->result_array();
		}
		$data['kuisioner'] = $kuisioner;
		$data['pesan'] = $this->session->flashdata('pesan');
		$this->load->view('cs/isi_kuisioner', $data);
	}
	
	function save_vote_kuisioner(){
		$kuisioner = $this->kuisioner->get(array('status' => STATUS_KUISIONER_PUBLISH))->result_array();
		$success = true;
		foreach($kuisioner as $kuisioner){
			$vote = $this->input->post('vote'.$kuisioner['id_kuisioner']);
			$jml = $this->input->post('jumlah_vote'.$kuisioner['id_kuisioner']);			
			for($i=0; $i < count($vote); $i++){
				$success = $this->kuisioner->edit_pilihan_jawaban($vote[$i], array("jumlah_vote" => $jml[$i]+1)) && $success;
			}
		}
		if($success){
			$this->session->set_flashdata('pesan', array('success' =>true, 'pesan' => "Data Kuisioner berhasil disimpan. Terima kasih telah mengisi kuisioner kami."));
		}else{
			$this->session->set_flashdata('pesan', array('success' =>false, 'pesan' => "Data Kuisioner gagal disimpan."));
		}
		redirect(base_url()."index.php/cs/isi_kuisioner");
	}
	
}